<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 * @author Nguyen Van Phuong  <phuong.nguyen.itvn@gmail.com>
 */
abstract  class BaseAdminController extends SBaseController
{
	
	public $_toolbar=array();
	public $portlets1=array();
    public $portlets2=array();
    public $breadcrumbs;
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public function init()
	{
		Yii::app()->clientScript->registerCoreScript('jquery');
		parent::init();
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param mixed $id the ID of the model to be loaded
	 * @param string $modelClass the model class name
	 * @return GxActiveRecord the loaded model
	 */
	public function loadModel($key, $modelClass) {
			if (is_array($key)) {
			// The key is an array.
			// Check if there are column names indexing the values in the array.
			reset($key);
			if (key($key) === 0) {
				// There are no attribute names.
				// Check if there are multiple PK values. If there's only one, start again using only the value.
				if (count($key) === 1)
					return $this->loadModel($key[0], $modelClass);

				// Now we will use the composite PK.
				// Check if the table has composite PK.
				$tablePk = GxActiveRecord::model($modelClass)->getTableSchema()->primaryKey;
				if (!is_array($tablePk))
					throw new CHttpException(400, Yii::t('giix', 'Your request is invalid.'));

				// Check if there are the correct number of keys.
				if (count($key) !== count($tablePk))
					throw new CHttpException(400, Yii::t('giix', 'Your request is invalid.'));

				// Get an array of PK values indexed by the column names.
				$pk = GxActiveRecord::model($modelClass)->fillPkColumnNames($key);

				// Then load the model.
				$model = GxActiveRecord::model($modelClass)->findByPk($pk);
			} else {
				// There are attribute names.
				// Then we load the model now.
				$model = GxActiveRecord::model($modelClass)->findByAttributes($key);
			}
		} else {
			// The key is not an array.
			// Check if the table has composite PK.
			$tablePk = GxActiveRecord::model($modelClass)->getTableSchema()->primaryKey;
			if (is_array($tablePk)) {
				// The table has a composite PK.
				// The key must be a string to have a PK separator.
				if (!is_string($key))
					throw new CHttpException(400, Yii::t('giix', 'Your request is invalid.'));

				// There must be a PK separator in the key.
				if (stripos($key, GxActiveRecord::$pkSeparator) === false)
					throw new CHttpException(400, Yii::t('giix', 'Your request is invalid.'));

				// Generate an array, splitting by the separator.
				$keyValues = explode(GxActiveRecord::$pkSeparator, $key);

				// Start again using the array.
				return $this->loadModel($keyValues, $modelClass);
			} else {
				// The table has a single PK.
				// Then we load the model now.
				$model = GxActiveRecord::model($modelClass)->findByPk($key);
			}
		}

		// Check if we have a model.
		if ($model === null)
			throw new CHttpException(404, Yii::t('giix', 'The requested page does not exist.'));

		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel $model the model to be validated
	 * @param string $form the name of the form
	 */
	protected function performAjaxValidation($model, $form) {
		if (Yii::app()->request->isAjaxRequest && $_POST['ajax'] == $form) {
			echo GxActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	/**
	 * Finds the related primary keys specified in the form post.
	 * Only for HAS_MANY and MANY_MANY relations.
	 * @param array $form The post data.
	 * @param array $relations A list of model relations.
	 * @return array An array where the keys are the relation names (string) and the values arrays with the related model primary keys (int|string) or composite primary keys (array with pk name (string) => pk value (int|string)).
	 * Example of returned data:
	 * array(
	 *   'categories' => array(1, 4),
	 *   'tags' => array(array('id1' => 3, 'id2' => 7), array('id1' => 2, 'id2' => 0)) // composite pks
	 * )
	 * An empty array is returned in case there is no related pk data from the post.
	 */
	protected function getRelatedData($form, $relations) {
		$relatedPk = array();
		foreach ($relations as $relationName => $relationData) {
			if (isset($form[$relationName]) && (($relationData[0] == GxActiveRecord::HAS_MANY) || ($relationData[0] == GxActiveRecord::MANY_MANY)))
			$relatedPk[$relationName] = $form[$relationName] === '' ? null : $form[$relationName];
		}
		return $relatedPk;
	}
}